<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    .router-link-active{
      background: red;
    }
  </style>
</head>
<body>
  <div id="app">
    <button @click="enterHome">去首页</button>
    <button @click="enterNews">新闻</button>
    <button>关于我们</button>
    <h1>我是单页面应用</h1>
    <router-view></router-view>
  </div>
  <script src="vue.js"></script>
  <script src="vue-router.js"></script>
  <script>
    /* 
    
    /home home页
    /new news页
    /about about
     */
    const Home = {
      template: `
        <div>
          <h1> home页 </h1>
          {{ $route.params.a }}
        </div>
      `
    }
    const News = {
      template: `
        <div>
          <h1> 新闻页 </h1>
        </div>
      `
    }
    const About = {
      template: `
        <div>
          <h1> 关于我们页 </h1>
        </div>
      `
    }
    // 以上称为 路由组件 需要在 路由 配置中 挂载
    // 定义路由
    const routes = [ // 路由规则
     /*  {
        path: '/',
        component: Home
      }, */
      {
        path: '/',
        redirect: '/news'
      },
      {
        path: '/home',
        name: '首页',
        component: Home
      },
      {
        path: '/news',
        name: '新闻',
        component: News
      },
      {
        path: '/about',
        name: '关于我们',
        component: About
      }
    ]
    const router = new VueRouter({
      routes
    })
    const vm = new Vue({
      el: '#app',
      methods: {
        enterHome(){
          console.log(this.$router)
          // this.$router.push('/home')
          /* this.$router.push({
            name:"首页",
            params:{
              a:10
            }
          }) */
          this.$router.push({
            path: '/home',
            query: {
              a:10
            }
          })
        },
        enterNews(){
          this.$router.push('/news')
        }
      },
      router
    })
  </script>
</body>
</html>